package com.jsgt.greekfire.ui.trading

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.ui.home.adapter.MarketAdapter
import com.jsgt.greekfire.viewmodel.CoinViewModel
import kotlinx.android.synthetic.main.activity_search_coin.*

/**
 * 搜索币种
 * author fhx
 * create time 2020/4/23 17:39
 **/
class SearchCoinActivity : BaseActivity() {
    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }

    override fun getLayoutId(): Int = R.layout.activity_search_coin

    override fun main() {
        search_group.setPadding(0, statusBarHeight, 0, 0)
        search_edit.requestFocus()
        cancel.setOnClickListener { finish() }
        val marketAdapter = MarketAdapter()
        search_rv.adapter = marketAdapter
        search_edit.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (!TextUtils.isEmpty(s)) {
                    coinViewModel.searchMarketList(s.toString())
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        coinViewModel.searchListLiveData.observe(this, Observer {
            marketAdapter.refreshData(it)
        })
    }
}